package com.treasure.mall.zbqmallmanager.controller.module;

import com.alibaba.fastjson.JSONObject;
import com.treasure.mall.biz.bo.module.HomeConfig;
import com.treasure.mall.biz.bo.module.HomeGoodsConfig;
import com.treasure.mall.biz.bo.module.HomeIconConfig;
import com.treasure.mall.biz.bo.module.HomeTodayNewConfig;
import com.treasure.mall.biz.entity.module.ModulePO;
import com.treasure.mall.biz.service.core.module.ModuleService;
import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbq.common.ienum.banner.ModuleType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * <p>
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.zbqmallmanager.controller.module
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/8/10 10:40 上午
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 * 
 */
@Controller
@RequestMapping("/module")
public class ModuleController {

    @Autowired
    private ModuleService moduleService;

    @RequestMapping("/home")
    public String home(Model model) {
        model.addAttribute("homeModel", moduleService.getConfig(ModuleType.MODULE_HOME));
        return "module/home";
    }

    @RequestMapping("/home/icon")
    public String homeIcon(Model model) {
        model.addAttribute("homeModel", moduleService.getConfig(ModuleType.MODULE_ICON));
        return "module/home_icon";
    }

    @RequestMapping("/home/today/new")
    public String homeTodayNew(Model model) {
        model.addAttribute("homeModel", moduleService.getConfig(ModuleType.MODULE_TODAY_NEW));
        return "module/home_today_new";
    }

    /**
     * 保存配置
     *
     * @return
     */
    @PostMapping("/home/edit")
    @ResponseBody
    public ApiResult editHome(@RequestParam(required = false, defaultValue = "0") Long id,
                              @RequestParam String title,
                              @RequestParam String url1,
                              @RequestParam String url2,
                              @RequestParam String url3,
                              @RequestParam String url4,
                              @RequestParam String url5,
                              @RequestParam String title1,
                              @RequestParam Long goodsId1,
                              @RequestParam Long goodsId2,
                              @RequestParam Long goodsId3,
                              @RequestParam Long goodsId4,
                              @RequestParam Long goodsId5,
                              @RequestParam String afterUrl2,
                              @RequestParam String afterUrl3,
                              @RequestParam String afterUrl4,
                              @RequestParam String afterUrl5

    ) {
        ModulePO po = null;
        if (id != null) {
            po = moduleService.findById(id);
        }
        if (po == null) {
            po = new ModulePO();
        }
        po.setTimingContent(title);
        po.setUpdateTime(LocalDateTime.now());
        HomeConfig homeConfig = new HomeConfig();
        homeConfig.setTitle(title1);
        homeConfig.setVideoUrl(url1);
        homeConfig.setGoodsId(goodsId1);
        List<HomeGoodsConfig> goodsConfigs = new ArrayList<>();
        goodsConfigs.add(new HomeGoodsConfig(goodsId2, url2, afterUrl2));
        goodsConfigs.add(new HomeGoodsConfig(goodsId3, url3, afterUrl3));
        goodsConfigs.add(new HomeGoodsConfig(goodsId4, url4, afterUrl4));
        goodsConfigs.add(new HomeGoodsConfig(goodsId5, url5, afterUrl5));
        homeConfig.setGoodsList(goodsConfigs);
        po.setProperties(JSONObject.toJSONString(homeConfig));
        po.setModuleType(homeConfig.getModuleType());
        po.setModuleName(homeConfig.getModuleType().getDesc());
        moduleService.save(po);
        return ApiResult.ok();
    }


    /**
     * 保存专题配置
     *
     * @return
     */
    @PostMapping("/home/icon/edit")
    @ResponseBody
    public ApiResult editHomeIcon(@RequestParam(required = false, defaultValue = "0") Long id,
                                  @RequestParam String url1,
                                  @RequestParam String url2,
                                  @RequestParam String specId1,
                                  @RequestParam String specId2,
                                  @RequestParam String title1,
                                  @RequestParam String title2


    ) {
        ModulePO po = null;
        if (id != null) {
            po = moduleService.findById(id);
        }
        if (po == null) {
            po = new ModulePO();
        }
        po.setUpdateTime(LocalDateTime.now());
        List<HomeIconConfig> homeIconConfigs = new ArrayList<>();
        homeIconConfigs.add(new HomeIconConfig(specId1, url1, title1));
        homeIconConfigs.add(new HomeIconConfig(specId2, url2, title2));
        po.setProperties(JSONObject.toJSONString(homeIconConfigs));
        po.setModuleType(ModuleType.MODULE_ICON);
        po.setModuleName(ModuleType.MODULE_ICON.getDesc());
        moduleService.save(po);
        return ApiResult.ok();
    }


    /**
     * 保存今日上新配置
     *
     * @return
     */
    @PostMapping("/home/today/new/edit")
    @ResponseBody
    public ApiResult editHomeTodayNew(@RequestParam(required = false, defaultValue = "0") Long id,
                                      @RequestParam String url1,
                                      @RequestParam String url2,
                                      @RequestParam String url3,
                                      @RequestParam String url4,
                                      @RequestParam String title1,
                                      @RequestParam String title2,
                                      @RequestParam String title3,
                                      @RequestParam String title4,
                                      @RequestParam String link
    ) {
        ModulePO po = null;
        if (id != null) {
            po = moduleService.findById(id);
        }
        if (po == null) {
            po = new ModulePO();
        }
        po.setUpdateTime(LocalDateTime.now());
        List<HomeTodayNewConfig> todayNewConfigs = new ArrayList<>();
        todayNewConfigs.add(new HomeTodayNewConfig(title1, url1, link));
        todayNewConfigs.add(new HomeTodayNewConfig(title2, url2, link));
        todayNewConfigs.add(new HomeTodayNewConfig(title3, url3, link));
        todayNewConfigs.add(new HomeTodayNewConfig(title4, url4, link));
        po.setProperties(JSONObject.toJSONString(todayNewConfigs));
        po.setModuleType(ModuleType.MODULE_TODAY_NEW);
        po.setModuleName(ModuleType.MODULE_TODAY_NEW.getDesc());
        moduleService.save(po);
        return ApiResult.ok();
    }

}
